<?php
include 'includes/global.php';
set_time_limit(0);
error_reporting(0);
//error_reporting(E_ALL);

switch ($_REQUEST['op']) {
    /**
     * 保存高级搜索参数
     */
    case 'saveParam':
        $_SESSION['search_advance'] = $_POST;
        break;

    /**
     * 导出文件
     */
    default:
        //将保存的高级搜索读取出来
        $_POST = $_SESSION['search_advance'];
        //清楚保存数据
        unset ($_SESSION['search_advance']);

        //文件名
        $file_name = 'ExcelOutput.' . date('Ymd');

        //输出文件
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $file_name . '.csv"');
        header('Cache-Control: max-age=0');
        //获取字段
        $in_fields = str_replace(',', "','", $_GET['fields']);
        $flds = Dgs('fld', "del!=1 and tid='{$_GET['tid']}' and id in ('{$in_fields}') order by px");

        //列名
        $column_name = array('id');
        foreach ($flds as $v) {
            $column_name[] = $v['name'];
            $field_name[] = $v['fname'];
        }
        //增加ID字段
        echo iconv('utf-8', 'gbk', implode(',', $column_name)) . "\n";

        /**
         * 高级搜索条件
         */
        $gjsc = array();
        foreach ($flds as $key => $value) {
            if ($value['gj'] == 1) {
                $gjsc[] = $value;
            }
        }
        $condition = '';
        $_POST['title'] && $condition.="and title like '%" . $_POST['title'] . "%' ";
        $_POST['bdtid'] && $condition.="and bdtid = '" . $_POST['bdtid'] . "' ";
        $_POST['bdid'] && $condition.="and bdid = '" . $_POST['bdid'] . "' ";
        if ($gjsc) {
            $xzt = array('combo', 'combo1', 'recommend', 'radiogroup');
            foreach ($gjsc as $value) {
                //字段配置
                $config_cfg = json_decode($value['cfg'], true);
                //单选、推荐、静态下拉
                if (in_array($value['type'], $xzt)) {
                    $_POST[$value['fname']] != '' && $condition.="and " . $value['fname'] . " = '" . $_POST[$value['fname']] . "' ";
                } else {
                    //多选
                    if ($value['type'] == 'checkboxgroup') {
                        if ($_POST[$value['fname']]) {
                            foreach ($_POST[$value['fname']] as $cv) {
                                $cv != '' && $condition.="and " . $value['fname'] . " like '%" . $cv . "%' ";
                            }
                        }
                        //区间搜索
                    } else if (true || $config_cfg['qj'] == 1 || $value['type'] == 'my97') {
                        //区间开始
                        if ('' != $_POST["{$value['fname']}_range"]) {
                            $range_start = $_POST["{$value['fname']}_range"];
                            //将my97转换为时间戳
                            if ($value['type'] == 'my97' && $value['zy'] != 1) {
                                $range_start = strtotime($_POST["{$value['fname']}_range"]);
                            }
                            $condition .= " and {$value['fname']}>='{$range_start}' ";
                        }
                        //区间结束
                        if ('' != $_POST["{$value['fname']}_range2"]) {
                            $range_end = $_POST["{$value['fname']}_range2"];
                            //将my97转换为时间戳
                            if ($value['type'] == 'my97' && $value['zy'] != 1) {
                                $range_end = strtotime($_POST["{$value['fname']}_range2"]);
                            }
                            $condition .= " and {$value['fname']}<='{$range_end}' ";
                        }

                        //普通匹配
                    } else {
                        $_POST[$value['fname']] != '' && $condition.="and " . $value['fname'] . " like '%" . $_POST[$value['fname']] . "%' ";
                    }
                }
            }
        }
        

        //导出内容
        $tree = Dga('tree', "id='{$_GET['tid']}'");
        $datamod = Dga('datamod', "id='{$tree['table']}'");
        $yachts = Dgs($datamod['tabname'], "del!=1 {$condition} order by id desc");
        foreach ($yachts as &$v) {
            //增加ID字段
            echo $v['id'].',';
            $val_arr = array();
            /**
             * 根据字段输出数据
             */
            foreach ($flds as $k2 => $v2) {

                switch ($v2['type']) {
                    case 'datefield':
                    case 'my97':
                        $config = json_decode($v2['cfg'], true);
                        if ($v[$v2['fname']] != '' && is_numeric($v[$v2['fname']]) && $config['zqgs'] != '') {
                            $v[$v2['fname']] = date($config['zqgs'], $v[$v2['fname']]);
                        }
                        break;
                    case 'radiogroup': {
                            $tempxx = json_decode($v2['zyz'], true);
                            foreach ($tempxx as $value) {
                                if ($value['yv'] == $v[$v2['fname']]) {
                                    $v[$v2['fname']] = $value['xv'];
                                }
                            }
                            break;
                        }
                    case 'checkboxgroup': {
                            $tempxx = json_decode($v2['zyz'], true);
                            $nowv = explode('#_#', $v[$v2['fname']]);
                            $v[$v2['fname']] = '';
                            foreach ($tempxx as $value) {
                                if (in_array($value['yv'], $nowv)) {
                                    $v[$v2['fname']].=$value['xv'] . "<BR>";
                                }
                            }
                            break;
                        }
                    case 'combo': {
                            $tempxx = json_decode($v2['zyz'], true);
                            foreach ($tempxx as $value) {
                                if ($value['yv'] == $v[$v2['fname']]) {
                                    $v[$v2['fname']] = $value['xv'];
                                }
                            }
                            break;
                        }
                    case 'combo1': {
                            $config = json_decode($v2['cfg'], true);
                            if ($config['y'] != '') {
                                $tq = Dgd(array(
                                    'db' => 'd_sevenel',
                                    'tn' => $config['table'],
                                    'cd' => " " . $config['y'] . " = '" . $v[$v2['fname']] . "' and  " . $config['conditon'],
                                    'limit' => 1
                                        ));
                                $v[$v2['fname']] = $tq[$config['x']];
                            }
                            break;
                        }
                    case 'combo2': {
                            $tq = Dgd(array(
                                'db' => 'd_sevenel',
                                'tn' => 'sort',
                                'cd' => " id = '" . $v[$v2['fname']] . "' ",
                                'limit' => 1
                                    ));
                            $v[$v2['fname']] = $tq['title'];
                            break;
                        }
                    case 'combo3': {
                            $tq = Dgd(array(
                                'db' => 'd_sevenel',
                                'tn' => 'sort',
                                'cd' => " id = '" . $v[$v2['fname']] . "' ",
                                'limit' => 1
                                    ));
                            $v[$v2['fname']] = $tq['title'];
                            break;
                        }

                    case 'recommend': {
                            $config = json_decode($v2['cfg'], true);
                            $bdid_con = $v2['bdid'] != "" ? "&bdid={$v2['bdid']}" : '';
                            if ($v[$v2['fname']] != 0) {
                                $v[$v2['fname']] = "真";
                            } else {
                                $v[$v2['fname']] = "假";
                            }
                            break;
                        }
                }
                $val_arr[] = str_replace(',','，',$v[$v2['fname']]);
            }
            echo iconv('utf-8', 'gbk', implode(',', $val_arr)) . "\n";
        }
        break;
}


